import java.util.*;


class ListNode {
    int val;
   ListNode next = null;
public ListNode(int val) {
     this.val = val;
   }
}


public class code2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param lists ListNode类ArrayList 
     * @return ListNode类
     */
    public ListNode mergeKLists (ArrayList<ListNode> lists) {
        PriorityQueue<ListNode> heap = new PriorityQueue<>((l1,l2)->{
            return l1.val-l2.val;
        });
        for(int i=0;i<lists.size();i++) {
            if(lists.get(i)!=null) {
                heap.offer(lists.get(i));
            }
        }
        ListNode ret = new ListNode(0);
        ListNode prev = ret;
        while(!heap.isEmpty()) {
            ListNode t = heap.poll();
            prev = prev.next = t;
            if(t.next!=null) {
                heap.offer(t.next);
            }
        }
        return ret.next;
    }
}